package tcpFirmware.controller;

import tcpFirmware.ResponseBody;
import tcpFirmware.Routes;
import tcpFirmware.Type;
import tcpFirmware.UploadPackage;
import tcpFirmware.model.DBHelper;
import tcpFirmware.model.Serial;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class UploadFilmController {
    @Routes(route = "upload/newFilm")
    public void newFilm(Socket socket, HashMap<String,String> head, byte[] bytes){
        UploadPackage pack = Serial.byteToObject(bytes);
        assert pack != null;
        System.out.println(pack.getDetail());
        String filepath = "src/tcpFirmware/image/"+pack.getFilename();
        SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
        String marketTime = df.format(new Date());
        String mvName = head.get("mvName");

        try {


            ResponseBody responseBody = new ResponseBody(socket);
            responseBody.setType(Type.json.type).setRoute("upload/newFilm").setData("data","success");
            responseBody.sendResponse();
            Connection c = new DBHelper().getConnection();
            Statement state = c.createStatement();
            int i=state.executeUpdate("insert into Movie values('"+mvName+"','"+marketTime+"','"+pack.getDetail()+"','"+head.get("price")+"','"+filepath+"','"+pack.getFilename()+"')");
            System.out.println(i);
            StringBuilder is = new StringBuilder("insert into Center values");
            ArrayList<HashMap<String,String>> data = pack.getData();
            for(HashMap<String,String> j:data){
                is.append("('").append(mvName).append("','").append(j.get("session")).append("','")
                        .append(j.get("center")).append("','").append(head.get("time")).append("','").append(j.get("beginTime")).append("'),");

            }
            is.deleteCharAt(is.lastIndexOf(","));
            state.executeUpdate(is.toString());

            state.close();
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    @Routes(route = "upload/film_image")
    public void uploadImage(Socket socket, HashMap<String,String> head, byte[] bytes){
        String filepath = "src/tcpFirmware/image/"+head.get("filename");
        try {
            if(head.get("uploadProgress").equals("done")){
                ResponseBody responseBody = new ResponseBody(socket);
                responseBody.setType(Type.text.type).setRoute("upload/film_image").setHeader("progress","done");
                responseBody.sendResponse();
            }else {
                ResponseBody responseBody = new ResponseBody(socket);
                responseBody.setType(Type.text.type).setRoute("upload/film_image").setHeader("progress","processing");
                responseBody.sendResponse();
                FileOutputStream outputStream = new FileOutputStream(filepath,true);
                outputStream.write(bytes);
                outputStream.flush();
                outputStream.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
